var BootstrapDialog,toastr,PerfectScrollbar,contentWidth;
/**
 *主入口文件
 */
/**
 * 配置require参数
 */
require.config({
	baseUrl: '/formal/',
	map: {
		'*': {
			'css': '/formal/css.min.js'
		}
	},
	paths : {
		'jquery' : 'lib/jquery1.11/js/jquery.min',
		'bootstrap' : 'lib/bootstrap3.3.7/js/bootstrap.min',
		'moment': 'lib/jquery1.11/js/moment.min',
		'metisMenu' : 'assets/plugins/metisMenu/metisMenu.min',
		'contextmenu':'lib/bootstrap3.3.7/js/bootstrap-contextmenu',
		'MultipleTreeSelect' : 'lib/jquery1.11/js/MultipleTreeSelect.min',
		'bootstrap-select': 'lib/bootstrap-select3/js/bootstrap-select.min',
		'defaults-zh_CN': 'lib/bootstrap-select3/js/defaults-zh_CN.min',
		'bootstrap-dialog': 'lib/bootstrap-dialog3/js/bootstrap-dialog.min',
		'bootstrap-switch': 'lib/bootstrap-switch3/js/bootstrap-switch.min',
		'bootstrap-datetimepicker': 'lib/bootstrap-datetimepicker3/js/bootstrap-datetimepicker.min',
		'bootstrap-datepicker_zh': 'lib/bootstrap-datetimepicker3/js/bootstrap-datetimepicker.zh-CN',
		'bootstrapValidator': 'https://cdn.bootcss.com/bootstrap-validator/0.5.3/js/bootstrapValidator.min',
		'star': 'lib/jquery1.11/star/js/star-rating.min',
		'toastr' : 'lib/jquery1.11/toastr/js/toastr',
		'jqGrid': 'lib/jquery1.11/jqGrid5.2/js/jquery.jqGrid.min',
		'jqGrid-locale':'lib/jquery1.11/jqGrid5.2/js/grid.locale-cn',
		'jszip': 'lib/jquery1.11/jqGrid5.2/js/jszip.min',
		'common':"assets/main/js/common",
		'cropper': 'lib/jquery1.11/cropper/js/cropper',
		'canvas-to-blob': 'lib/jquery1.11/cropper/js/canvas-to-blob.min',
		'fileInput': 'lib/bootstrap-fileinput/js/fileinput.min',
		'fileInput_zh': 'lib/bootstrap-fileinput/js/zh',
        'perfect-scrollbar': 'assets/plugins/perfect-scrollbar/perfect-scrollbar.min',
        'Chart': 'assets/plugins/chartJs/Chart.min',
        'sparkline': 'assets/plugins/sparkline/sparkline.min',
        'dashboard': 'assets/dist/js/pages/dashboard',
        'sidebar': 'assets/dist/js/sidebar'
	},
	//require 映射关系依赖
	shim : {
        'perfect-scrollbar' : {
            deps: ['jquery']
        },
        'Chart' : {
            deps: ['jquery']
        },
        'sparkline' : {
            deps: ['jquery']
        },
        'dashboard' : {
            deps: ['jquery','sparkline']
        },
        'sidebar' : {
            deps: ['jquery','metisMenu']
        },
		'moment': {
			deps: ['jquery']
		},
		'bootstrap' : {
			deps : ['jquery']
		},
		'metisMenu' : {
			deps : ['jquery','bootstrap']
		},
		'bootstrap-dialog': {
			deps : ['css!lib/bootstrap-dialog3/css/bootstrap-dialog.min.css','jquery','bootstrap']
		},
		'bootstrap-switch': {
			deps : ['jquery','bootstrap','css!lib/bootstrap-switch3/css/bootstrap-switch.min.css']
		},
		'bootstrap-datetimepicker':{
			deps : ['css!lib/bootstrap-datetimepicker3/css/bootstrap-datetimepicker.min.css','jquery','bootstrap']
		},
		'bootstrap-datepicker_zh':{
			deps : ['bootstrap-datetimepicker','jquery']
		},
		'fileInput':{
			deps : ['jquery','bootstrap','css!lib/bootstrap-fileinput/css/fileinput.min.css']
		},
		'fileInput_zh':{
			deps : ['jquery','fileInput']
		},
		'bootstrapValidator':{
			deps : ['css!https://cdn.bootcss.com/bootstrap-validator/0.5.3/css/bootstrapValidator.min.css','jquery','bootstrap']
		},
		'star' : {
			deps : ['css!lib/jquery1.11/star/css/star-rating.min.css','jquery']
		},
		'toastr' : {
			deps : ['css!lib/jquery1.11/toastr/css/toastr.css','jquery']
		},
		'defaults-zh_CN':{
			deps : ['jquery','bootstrap','bootstrap-select']
		},
		'bootstrap-select':{
			deps : ['css!lib/bootstrap-select3/css/bootstrap-select.min.css','jquery','bootstrap']
		},
		'MultipleTreeSelect' : {
			deps : ['jquery']
		},
		'jqGrid':{
			deps : ['css!lib/jquery1.11/jqGrid5.2/css/ui.jqgrid-bootstrap.css','jquery']
		},
		'jqGrid-locale':{
			deps : ['jquery']
		},
		'jszip':{
			deps: ['jquery','jqGrid']
		},
		'common':{
			deps : ['jquery']
		},
		'canvas-to-blob': {
			deps: ['jquery']
		},
		'cropper':{
			deps: ['css!lib/jquery1.11/cropper/css/cropper.css','jquery','canvas-to-blob']		
		}
	}  
});

require([
'jqGrid','bootstrap','bootstrap-datetimepicker','bootstrapValidator',
'perfect-scrollbar','Chart','sparkline','dashboard','sidebar','bootstrap-switch',
'jqGrid-locale','toastr','metisMenu','defaults-zh_CN','bootstrap-dialog','common'],function(){
	BootstrapDialog = require('bootstrap-dialog');
    PerfectScrollbar = require('perfect-scrollbar');
    $('.sidebar-body').each(function () {
        new PerfectScrollbar($(this)[0]);
    });
    contentWidth = $("#formal-body-content").height();
	initJqGrid();
	initWindow();
	initToast();
	initMenu();
	//菜单样式
	function initMenu(){
		$('#side-menu li').click(function(e){
			var id = $(this).find('a').attr('data-id');
			if(id){
				$.ajax({
					url:'/resource/list/'+id,
					success: function(data){
						if(data && data.url){
                            $.ajax({
                                url: data.url,
                                success: function (text) {
                                    $("#formal-body-content").html(text);
                                }
                            });
						}
					}
				});
			}else if (id == 'load'){
				window.location.reload();
			}
		});
	}
	/**
	 * 初始化消息提示框
	 */
	function initToast(){
		toastr = require('toastr');
		toastr.options = {
			'closeButton': false, //是否显示关闭按钮
			'debug': false, //是否使用debug模式
			'positionClass': 'toast-top-center',//弹出窗的位置
			'showDuration': '300',//显示的动画时间
			'hideDuration': '500',//消失的动画时间
			'timeOut': '3000', //展现时间
			'extendedTimeOut': '500',//加长展示时间
			'showEasing': 'swing',//显示时的动画缓冲方式
			'hideEasing': 'linear',//消失时的动画缓冲方式
			'showMethod': 'fadeIn',//显示时的动画方式
			'hideMethod': 'fadeOut' //消失时的动画方式
		};
		//data 指的是返回的resultData
		toastr.result = function(data,callback){
			if(data.success){
				toastr.success(data.message);
				if($.isFunction(callback)){
					callback();
				}
			}else{
				toastr.error(data.message);
			}
		}
	}

	/**
	 * 设置未来(全局)的AJAX请求默认选项
	 * 主要设置了AJAX请求遇到Session过期的情况
	 * 使用 complete 捕获结果，做超时处理
	 */
	function initWindow(){
		$.ajaxSetup({
			complete: function (xhr, status) {
				if(xhr.status == 404){
					var id = $('#maintab .active').find('a').attr('href');
					if(id){
						$(id).html(xhr.responseText);
					}
				}else{
					var sessionStatus = xhr.getResponseHeader("sessionstatus");
			        if (sessionStatus == 'timeout') {
			        	window.location.href="/fashion/index";
			        }
				}
			}
		});
		//禁用回退按钮
		history.pushState(null, null, document.URL);
        window.addEventListener('popstate', function () {
            history.pushState(null, null, document.URL);
        });
        //禁用双击选中 不限制文本域（input等）
        $(document).bind("selectstart",function(){return false;});
	}

	/**
	 * 在页面中任何嵌套层次的窗口中获取顶层窗口
	 * @return 当前页面的顶层窗口对象
	 */
	function getTopWinow(){
		var p = window;
		while(p != p.parent){
			p = p.parent;
		}
		return p;
	}

	/**
	 * 重写jqGrid默认参数
	 */
	function initJqGrid(){
		$.jgrid.defaults.styleUI = 'Bootstrap';
		$.jgrid.defaults.datatype = 'json';
		$.jgrid.defaults.responsive = true;
		$.jgrid.defaults.prmNames = {page:"pageNum",rows:"pageSize",sort: "order",order:"sort"};
	}
	
});


